home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianGeometry.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  262 lines

  1. /*ScianGeometry.c
  2.   Eric Pepke
  3.   8 Feb 1993
  4.   New geometry stuff within SciAn
  5. */
  6.  
  7. #include "Scian.h"
  8. #include "ScianTypes.h"
  9. #include "ScianNames.h"
  10. #include "ScianGeometry.h"
  11. #include "ScianIDs.h"
  12. #include "ScianArrays.h"
  13. #include "ScianLists.h"
  14. #include "ScianWindows.h"
  15. #include "ScianObjWindows.h"
  16. #include "ScianPictures.h"
  17. #include "ScianDatasets.h"
  18. #include "ScianSpaces.h"
  19.  
  20. ObjPtr geoObjClass;            /*Class of all geometry objects*/
  21.  
  22. ObjPtr NewGeometry()
  23. /*Returns a new geometry object*/
  24. {
  25.     ObjPtr retVal;
  26.  
  27.     retVal = NewObject(geoObjClass, sizeof(Geometry) - sizeof(Thing));
  28.     retVal -> flags = OT_GEOMETRY;
  29.     ((GeoPtr) retVal) -> items = NULL;
  30.     ((GeoPtr) retVal) -> nAlloc = 0;
  31.     ((GeoPtr) retVal) -> nWritten = 0;
  32. }
  33.  
  34. ObjPtr CleanupGeometry(geo)
  35. ObjPtr geo;
  36. /*Cleans up a geometry object*/
  37. {
  38.     if (((GeoPtr) geo) -> items)
  39.     {
  40.     Free(((GeoPtr) geo) -> items);
  41.     ((GeoPtr) geo) -> items = NULL;
  42.     }
  43.     return ObjTrue;
  44. }
  45.  
  46. #ifdef PROTO
  47. static AppendLongToGeometry(ObjPtr geometry, long item)
  48. #else
  49. static AppendLongToGeometry(geometry, item)
  50. ObjPtr geometry;
  51. long item;
  52. #endif
  53. {
  54.     if (((GeoPtr) geometry) -> nWritten >=
  55.     ((GeoPtr) geometry) -> nAlloc)
  56.     {
  57.     /*Have to Reallocate some stuff*/
  58.     if (((GeoPtr) geometry) -> nAlloc)
  59.     {
  60.         ((GeoPtr) geometry) -> nAlloc += GEOCHUNK;
  61.         ((GeoPtr) geometry) -> items = Realloc(((GeoPtr) geometry) -> items, ((GeoPtr) geometry) -> nAlloc * sizeof(GeoItem));
  62.     }
  63.     else
  64.     {
  65.         ((GeoPtr) geometry) -> nAlloc += GEOCHUNK;
  66.         ((GeoPtr) geometry) -> items = Alloc(((GeoPtr) geometry) -> nAlloc * sizeof(GeoItem));
  67.     }
  68.     }
  69.     ((GeoPtr) geometry) -> items[((GeoPtr) geometry) -> nWritten] . intVal = item;
  70.     ++(((GeoPtr) geometry) -> nWritten);
  71. }
  72.  
  73. #ifdef PROTO
  74. void AppendPolygonToGeometry(ObjPtr geometry, long nVertices, long vertices[])
  75. #else
  76. void AppendPolygonToGeometry(geometry, nVertices, vertices)
  77. ObjPtr geometry;
  78. long nVertices;
  79. long vertices[];
  80. #endif
  81. /*Appends a polygon to a geometry frame*/
  82. {
  83.     long k;
  84.     AppendLongToGeometry(geometry, IT_POLYGON);
  85.     AppendLongToGeometry(geometry, nVertices);
  86.     for (k = 0; k < nVertices; ++k)
  87.     {
  88.     AppendLongToGeometry(geometry, vertices[k]);
  89.     }
  90. }
  91.  
  92. #ifdef PROTO
  93. ObjPtr ConvertDatasetToPicture(ObjPtr dataset, ObjPtr normalsObj)
  94. #else
  95. ObjPtr ConvertDatasetToPicture(dataset, normalsObj)
  96. ObjPtr dataset;
  97. ObjPtr normalsObj;
  98. #endif
  99. /*Converts a dataset to a picture.  normalsObj may provide normals*/
  100. {
  101.     GeoPtr geometry;
  102.     long k;
  103.     long dim;
  104.     long curItemWord;
  105.     long nVertices;
  106.     ObjPtr retVal;
  107.     VertexPtr *vertices;
  108.     long nTempVertices;
  109.     VertexPtr *tempVertices;
  110.     PolysPtr polys;
  111.     real position[3], normal[3];
  112.     Bool equalNormalForm;
  113.     ObjPtr var;
  114.  
  115.     SetCurForm(FORMFIELD, dataset);
  116.     SetCurField(FIELD1, dataset);
  117.  
  118.     geometry = (GeoPtr) curFields[FIELD1] . objectInfo;
  119.     if (!geometry || !IsGeometry(geometry))
  120.     {
  121.     ReportError("ConvertDatasetToPicture", "No valid geometry");
  122.     return NULLOBJ;
  123.     }
  124.  
  125.     if (CountTraversalDims(FORMFIELD) != 1)
  126.     {
  127.     ReportError("ConvertDatasetToPicture", "Bad data form");
  128.     return NULLOBJ;
  129.     }
  130.  
  131.     GetTraversalDims(FORMFIELD, &dim);
  132.  
  133.     /*See if normals are set and the same*/
  134.     equalNormalForm = false;
  135.     if (normalsObj)
  136.     {
  137.     SetCurField(FIELD1, normalsObj);
  138.     if (IdenticalFields(FORMFIELD, FIELD1))
  139.     {
  140.         equalNormalForm = true;
  141.     }
  142.     }
  143.  
  144.     /*Create the picture*/
  145.     retVal = NewPicture();
  146.     if (!retVal) return NULLOBJ;
  147.  
  148.     polys = AppendPolysToPicture(retVal);
  149.  
  150.     /*Make temporary holder for vertices*/
  151.     vertices = (VertexPtr *) Alloc(dim * sizeof(VertexPtr));
  152.     if (!vertices) 
  153.     {
  154.     OMErr();
  155.     return NULLOBJ;
  156.     }
  157.  
  158.     /*Go through and make the vertices*/
  159.     for (k = 0; k < dim; ++k)
  160.     {
  161.     vertices[k] = NewVertex(retVal, k ? VF_NEXTCANON : VF_FIRSTCANON);
  162.     vertices[k] -> position[0] = SelectFieldComponent(FORMFIELD, 0, &k);
  163.     vertices[k] -> position[1] = SelectFieldComponent(FORMFIELD, 1, &k);
  164.      vertices[k] -> position[2] = SelectFieldComponent(FORMFIELD, 2, &k);
  165.     if (normalsObj)
  166.     {
  167.         if (equalNormalForm)
  168.         {
  169.         vertices[k] -> normal[0] = SelectFieldComponent(FIELD1, 0, &k);
  170.         vertices[k] -> normal[1] = SelectFieldComponent(FIELD1, 1, &k);
  171.          vertices[k] -> normal[2] = SelectFieldComponent(FIELD1, 2, &k);
  172.         }
  173.         else
  174.         {
  175.         position[0] = vertices[k] -> position[0];
  176.         position[1] = vertices[k] -> position[1];
  177.         position[2] = vertices[k] -> position[2];
  178.         SampleSpatComponent(FIELD1, FORMFIELD, 3,
  179.             normal, 3, position, true);
  180.         vertices[k] -> normal[0] = normal[0];
  181.         vertices[k] -> normal[1] = normal[1];
  182.         vertices[k] -> normal[2] = normal[2];
  183.         }
  184.     }
  185.     }
  186.  
  187.     /*Make temp vertices for producing polygons and stuff*/
  188.     tempVertices = (VertexPtr *) Alloc(20 * sizeof(VertexPtr));
  189.     nTempVertices = 20;
  190.  
  191.     /*Now parse the geometry*/
  192.     curItemWord = 0;
  193.     while (curItemWord < geometry -> nWritten)
  194.     {
  195.     switch(geometry -> items[curItemWord] . intVal)
  196.     {
  197.         case IT_POLYGON:
  198.         ++curItemWord;
  199.         nVertices = geometry -> items[curItemWord] . intVal;
  200.         ++curItemWord;
  201.         if (nVertices > nTempVertices)
  202.         {
  203.             nTempVertices = nVertices;
  204.             tempVertices = (VertexPtr *) Realloc(tempVertices, nTempVertices * sizeof(VertexPtr));
  205.         }
  206.         for (k = 0; k < nVertices; ++k)
  207.         {
  208.             tempVertices[k] = vertices[geometry -> items[curItemWord] . intVal];
  209.             ++curItemWord;
  210.         }
  211.         if (nVertices)
  212.         {
  213.             /*Append a polygon*/
  214.             AppendSPolyToPolys(polys, nVertices, tempVertices);
  215.         }
  216.         break;
  217.         default:
  218.         ReportError("ConvertDatasetToPicture", "Bad geometry item");
  219.         curItemWord = geometry -> nWritten;
  220.         break;
  221.     }
  222.     }
  223.  
  224.     /*Get rid of temporary vertex arrays*/
  225.     SAFEFREE(tempVertices);
  226.     SAFEFREE(vertices);
  227.  
  228.     if (!normalsObj)
  229.     {
  230.     CalcPictureNormals(retVal);
  231.     }
  232.  
  233.     return retVal;
  234. }
  235.  
  236. static ObjPtr RegisterGeoField(geo, whichField)
  237. ObjPtr geo;
  238. int whichField;
  239. /*Registers geo in a field*/
  240. {
  241.     curFields[whichField] . objectInfo = geo;
  242.     return ObjTrue;
  243. }
  244.  
  245. void InitGeometry()
  246. {
  247.     geoObjClass = NewObject(NULLOBJ, sizeof(Geometry) - sizeof(Thing));
  248.     geoObjClass -> flags = OT_GEOMETRY;
  249.     ((GeoPtr) geoObjClass) -> items = NULL;
  250.     ((GeoPtr) geoObjClass) -> nAlloc = 0;
  251.     ((GeoPtr) geoObjClass) -> nWritten = 0;
  252.     SetMethod(geoObjClass, CLEANUP, CleanupGeometry);
  253.     SetMethod(geoObjClass, REGISTERFIELD, RegisterGeoField);
  254.  
  255.     AddToReferenceList(geoObjClass);
  256. }
  257.  
  258. void KillGeometry()
  259. {
  260.     DeleteThing(geoObjClass);
  261. }
  262.